package com.example.design.personal.observer.jdk;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeListenerProxy;
import java.beans.PropertyChangeSupport;

public class Demo {

    private String name;

    private PropertyChangeSupport listeners  = new PropertyChangeSupport(this);

    public Demo() {
        this.name = "my name";
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        String oldValue = this.name;
        this.name = name;
        firePropertyChange("name2",oldValue,name);
    }

    public void addPropertyChangeListener(PropertyChangeListener listener) {
        listeners.addPropertyChangeListener(listener);
    }

    public void removePropertyChangeListener(PropertyChangeListener listener){
        listeners.removePropertyChangeListener(listener);
    }

    protected void firePropertyChange(String prop,String oldValue ,String newValue){

        listeners.firePropertyChange(prop,oldValue,newValue);
    }

    public static void main(String[] args) {
        Demo demo = new Demo();
       // demo.addPropertyChangeListener(new PropertyChangeListenerProxy());
        demo.addPropertyChangeListener(new PropertyChangeListener() {
            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                System.out.println("newValue = "+evt.getNewValue());
                if(evt.getNewValue().equals("xurongbei")){
                    System.out.println("找到组织了！");
                }
            }
        });
        demo.setName("xurongbei");
    }
}
